/**
 * easyui扩展
 */
(function($) {
	$.extend($.fn.form.methods, {
		/**
		 * getData 获取数据接口
		 * 
		 * @param {Object} jq
		 * @param {Object} params 设置为true的话，会把string型"true"和"false"字符串值转化为boolean型。
		 */
	    getData: function(jq, field){
	        var formArray = jq.serializeArray();
	        var oRet = {};
	        for (var i in formArray) {
	            if (typeof(oRet[formArray[i].name]) == 'undefined') 
	            	oRet[formArray[i].name] = formArray[i].value;
	            else  oRet[formArray[i].name] += "," + formArray[i].value;
	        }
	        if(field){
	        	return oRet[field];
	        }
	        return oRet;
	    },
	    
	    /**
	     * ajaxSubmit
	     * 
	     */
	    ajaxSubmit: function(target,params){
	    	if(params.validate!="false"&&params.validate!=false){
				var v = target.form('validate');
				if (!v) return false;
	    	}
	    	if(params.onSubmit&&(typeof params.onSubmit)=="function"){
	    		var v=params.onSubmit();
	    		if (!v) return false;
	    	}
	    	var mask = true;
	    	if(params.mask !== null && params.mask !== undefined ){
	    		mask = params.mask;
	    	}
	    	var data=target.form("getData");
	    	$.extend(data, params.data);
			$.ajax(params.url,{
				data:data,
				mask:mask,
				type: "POST",
				success:function(data) {
					if(params.success&& $.type(params.success)==="function"){
			    		params.success(data);
			    	}
				}
			});
	    }
	});
	
	
	var dateboxparse=$.fn.datebox.defaults.parser;
	
	$.extend($.fn.datebox.defaults, {
		parser: function(s){
			if (!s)
				return null;
			if($.type(s) === "number"){
				return new Date(s)
			}else{
				return dateboxparse(s);
			}
		},
		getText: function(){
			return "";
		}
	});
	
	$.extend($.fn.datetimebox.defaults, {
		parser: function(s){
			if (!s)
				return new Date();
			if($.type(s) === "number"){
				return new Date(s)
			}else{
				var dt=s.split(" ");
				var d=dateboxparse(dt[0]);
				if(dt.length<2){
				return d;
				}
				var _2d=$(this).datetimebox("spinner").timespinner("options").separator;
				var tt=dt[1].split(_2d);
				var _2e=parseInt(tt[0],10)||0;
				var _2f=parseInt(tt[1],10)||0;
				var _30=parseInt(tt[2],10)||0;
				return new Date(d.getFullYear(),d.getMonth(),d.getDate(),_2e,_2f,_30);
			}
		},
		getText: function(){
			return "";
		}
	});

	$.extend($.fn.numberbox.defaults, {
		formatter : function(_28) {
			if (!_28) {
                return _28;
            }
            _28 = _28 + "";
            var _29 = $(this).numberbox("options");
			if (_29.measure) {
				if(_29.measure=="percent"){
					_28 = parseFloat(_28) * 100 + "";
					_29.suffix = "%"
				} else if(_29.measure=="wan"){
					_28 = parseFloat(_28) / 10000 + "";
					_29.suffix = "万"
				} else if(_29.measure=="yi"){
					_28 = parseFloat(_28) / 10000/10000 + "";
					_29.suffix = "亿"
				}
			}
			_28 = parseFloat(_28).toFixed(_29.precision)+"";

            var s1 = _28, s2 = "";
            var _2a = _28.indexOf(".");
            if (_2a >= 0) {
                s1 = _28.substring(0, _2a);
                s2 = _28.substring(_2a + 1, _28.length);
            }
            if (_29.groupSeparator) {
                var p = /(\d+)(\d{3})/;
                while (p.test(s1)) {
                    s1 = s1.replace(p, "$1" + _29.groupSeparator + "$2");
                }
            }
            if (s2) {
                return _29.prefix + s1 + _29.decimalSeparator + s2 + _29.suffix;
            } else {
                return _29.prefix + s1 + _29.suffix;
            }
		},
		parser : function(s) {
			s = s + "";
			var containMeasure = Math.max(s.indexOf("%"), s.indexOf("万"), s.indexOf("亿"));
			var _2b = $(this).numberbox("options");
            if (parseFloat(s) != s) {
                if (_2b.prefix) {
                    s = $.trim(s.replace(new RegExp("\\" + $.trim(_2b.prefix), "g"), ""));
                }
                if (_2b.suffix) {
                    s = $.trim(s.replace(new RegExp("\\" + $.trim(_2b.suffix), "g"), ""));
                }
                if (_2b.groupSeparator) {
                    s = $.trim(s.replace(new RegExp("\\" + _2b.groupSeparator, "g"), ""));
                }
                if (_2b.decimalSeparator) {
                    s = $.trim(s.replace(new RegExp("\\" + _2b.decimalSeparator, "g"), "."));
                }
                s = s.replace(/\s/g, "");
            }
            var val = parseFloat(s);
            if (isNaN(val)) {
                val = "";
            } else {
                if (typeof (_2b.min) == "number" && val < _2b.min) {
                    val = _2b.min;
                } else {
                    if (typeof (_2b.max) == "number" && val > _2b.max) {
                        val = _2b.max;
                    }
                }
            }
            
			var options = $(this).numberbox("options");
			if (options.measure && containMeasure>-1) {
				if(options.measure=="percent"){
					val = val/100;
				} else if(options.measure=="wan"){
					val = val*10000;
				} else if(options.measure=="yi"){
					val = val*10000*10000;
				}
			}
			return val;
		}
	});

	var defualtCombobox = $.fn.combobox;
	$.fn.combobox = function(options, param){
		if (typeof options != 'string'){
			if(options.flexset){
				options = $.extend(options, {valueField:'code',textField:'name',data:frame.flex.getValue(options.flexset)});
			}
		}
		return defualtCombobox.call(this, options, param);
	};
	$.fn.combobox.methods = defualtCombobox.methods;
	$.fn.combobox.parseOptions = function(target){
		var options = $.extend({}, $.fn.combo.parseOptions(target), $.parser.parseOptions(target,[
			'valueField','textField','groupField','groupPosition','mode','method','url',{showItemIcon:'boolean'}
		]));
		if(options.flexset){
			options = $.extend(options, {valueField:'code',textField:'name',data:frame.flex.getValue(options.flexset)});
		}
		return options;
	};
	$.fn.combobox.parseData = defualtCombobox.parseData;
	$.fn.combobox.defaults = defualtCombobox.defaults;

})(jQuery);